#/bin/bash

# protoc 
PB_DIR=./grpc/pb
PB_GEN_DIR=./grpc/gen
PB_MODULE_DIRS=(shared util service)
PROTOC_GO_OPT=paths=source_relative
PROTOC_GO_GRPC_OPT=paths=source_relative

for MOD_NAME in ${PB_MODULE_DIRS[@]};
do
    MOD_PATH=$PB_DIR/$MOD_NAME
    MOD_OUTPUT_PATH=$PB_GEN_DIR/$MOD_NAME
    # echo $MOD_PATH
    # echo $MOD_OUTPUT_PATH
    for FILE in `ls $MOD_PATH | grep ".proto$"`
    do
      if [ ! -f "$FILE" ]; then
        
        FILE_PATH=$MOD_PATH/$FILE
        FILE_NAME=$(echo $FILE | sed 's/\.[^.]*$//')
        FILE_OUTPUT_PATH=$MOD_OUTPUT_PATH/$FILE_NAME
        echo $FILE_PATH
        echo $FILE_OUTPUT_PATH
        mkdir -p $FILE_OUTPUT_PATH
        protoc --proto_path=$PB_DIR --proto_path=$MOD_PATH \
				  --go_out=$FILE_OUTPUT_PATH --go_opt=$PROTOC_GO_OPT \
				  --go-grpc_out=$FILE_OUTPUT_PATH --go-grpc_opt=$PROTOC_GO_GRPC_OPT \
				  $FILE
      fi
    done;
done;